-- pro_ranged_damage
-- created by dengc
-- 使用道具时，概率变为范围伤害

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 触发
    trigger = function(who, prop, arg)
        local target = arg["target"];
        local targets = { target };

        local classId = arg["class_id"];
        if not classId then
            return targets;
        end

        -- 不是期望的道具
        if prop[2] ~= classId then
            return targets;
        end

        -- 概率触发
        if DungeonM.getRandSeed("pro_ranged_damage") % 1000 >= prop[3] then
            return targets;
        end

        local range = prop[4];

        -- 范围内的目标
        local grid, monster;
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end

        local coor = DungeonM.convertToCoor(pos);
        local c, near;
        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            c = DungeonM.convertToCoor(p);

            grid = DungeonM.getGridByPos(p);
            monster = grid.monster;

            -- 有效范围内
            near = math.abs(c.x - coor.x) + math.abs(c.y - coor.y) <= range;

            -- 翻开且存活的怪物，且不是初始目标
            if  near and grid:isOpened() and
                grid:isMonster() and
                not monster:isDead() and
                monster ~= target then
                table.insert(targets, monster);
            end
        end

        return targets;
    end,

    record = function(target, prop, path)
        return false;
    end
};
